package com.amos.cmp_android.presenter;

import android.content.Context;
import android.util.Log;

import com.amos.cmp_android.constant.URLConstant;
import com.amos.cmp_android.model.AcademyModel;
import com.amos.cmp_android.model.FacultyModel;
import com.amos.cmp_android.model.TeacherModel;
import com.amos.cmp_android.net.STError;
import com.amos.cmp_android.net.STHttpCallback;
import com.amos.cmp_android.net.STNetTask;
import com.amos.cmp_android.net.STRequest;
import com.amos.cmp_android.utils.GsonUtil;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by tlh on 2016/4/11.
 */
public class GetTeacherPresenter extends BasePresenter {
    private GetTeacherListener listener;
    private List<TeacherModel> list;

    public GetTeacherPresenter(Context context) {
        super(context);
    }
    public void setListener(GetTeacherListener listener, List<TeacherModel> list) {
        this.listener = listener;
        this.list = list;
    }
    public void getTeacher(int page,int num,int departmentId){
        Map<String, Object> params = new HashMap<>();
        STRequest request;
        params.put("page", page);
        params.put("num", num);
        params.put("did",departmentId);
        request = new STRequest(URLConstant.GET_TEACHER, params, null);
        STNetTask.post(request, new STHttpCallback<JSONObject>() {
            @Override
            public void onSuccess(String response) {
                list.clear();
                GsonUtil.fromJson(response,TeacherModel.class,list);
                if (listener!=null)
                    listener.onSuccess("teacher");
            }

            @Override
            public void onFailed(STError error) {
                if (listener != null)
                    listener.onFailed(error);
            }
        }, beforeQueryHook, afterQueryHook);
    }
    public void getTeacherByName(int page,int num,String name){
        Map<String, Object> params = new HashMap<>();
        STRequest request;
        params.put("page", page);
        params.put("num", num);
        params.put("name",name);
        request = new STRequest(URLConstant.GET_TEACHER_BY_NAME, params, null);
        STNetTask.post(request, new STHttpCallback<JSONObject>() {
            @Override
            public void onSuccess(String response) {
                list.clear();
                GsonUtil.fromJson(response,TeacherModel.class,list);
                if (listener!=null)
                    listener.onSuccess("byName");
            }

            @Override
            public void onFailed(STError error) {
                if (listener != null)
                    listener.onFailed(error);
            }
        }, beforeQueryHook, afterQueryHook);
    }
    public List<AcademyModel> getAcademy(int page, int num){
        Map<String, Object> params = new HashMap<>();
        final List<AcademyModel> academyModelList=new ArrayList<>();
        STRequest request;
        params.put("page", page);
        params.put("num", num);
        request = new STRequest(URLConstant.GET_ACADEMY, params, null);
        STNetTask.post(request, new STHttpCallback<JSONObject>() {
            @Override
            public void onSuccess(String response) {
                GsonUtil.fromJson(response,AcademyModel.class,academyModelList);
                if (listener!=null)
                    listener.onSuccess("academy");
            }
            @Override
            public void onFailed(STError error) {
                if (listener != null)
                    listener.onFailed(error);
            }
        }, beforeQueryHook, afterQueryHook);
        return academyModelList;
    }
    public List<FacultyModel> getFaculty(int page, int num, int aid){
        Map<String, Object> params = new HashMap<>();
        final List<FacultyModel> facultyModelList=new ArrayList<>();
        STRequest request;
        params.put("page", page);
        params.put("num", num);
        params.put("aid", aid);
        request = new STRequest(URLConstant.GET_FACULTY, params, null);
        STNetTask.post(request, new STHttpCallback<JSONObject>() {
            @Override
            public void onSuccess(String response) {
                Log.e("getFacultyPresenter", response);
                GsonUtil.fromJson(response,FacultyModel.class,facultyModelList);
                if (listener!=null)
                    listener.onSuccess("faculty");
            }
            @Override
            public void onFailed(STError error) {
                if (listener != null)
                    listener.onFailed(error);
            }
        }, beforeQueryHook, afterQueryHook);
        return facultyModelList;
    }
    public interface GetTeacherListener{
        public void onSuccess(String info);
        public void onFailed(STError error);
    }
}

